<?php

function is_duplicate_period_teacher($data = null) {
    $extra = "";
    global $period_info;

    global $teacher_info;

    print_r($data);
    print_r($_REQUEST);

//         $data["TEACHER_ID_"];
    $_REQUEST["course_id"];



//	     $extra=" and course_periods.course_period_id <>'{$data["id"]}' ";
    $sql = " select days as d,period_id as p_id ,marking_period_id from course_periods where course_id=" . $_REQUEST["course_id"] . " ";

    $course_periods = DBGet(DBQuery($sql));
    foreach ($course_periods as $course_period) {
        $res = $course_period;
        $array_of_days = "";
        foreach (str_split($res["D"]) as $day) {
            if (empty($array_of_days))
                $array_of_days.=" and ( course_periods.days like '%" . $day . "%'";
            else
                $array_of_days.=" or course_periods.days like '%" . $day . "%'";
        }
        if (!empty($array_of_days))
            $array_of_days.=")";
        $data["DAYS"] = $array_of_days;
        $data["PERIOD_ID"] = $res["P_ID"];
        $data["MARKING_PERIOD_ID"] = $res["MARKING_PERIOD_ID"];


        $extra = " and course_periods.course_period_id <>'{$data["PERIOD_ID"]}' ";

        $marking_periods_ids = array();
        $sql = "select * from marking_periods where marking_periods.SCHOOL_ID='" . UserSchool() . "' AND marking_periods.SYEAR='" . UserSyear() . "' and marking_periods.marking_period_id='" . $data["MARKING_PERIOD_ID"] . "'  ";

        $has_grand = false;
        $res = DBGet(DBQuery($sql));
        $res = $res[1];

        if ($res["PARENT_ID"] != "-1") {
            $marking_periods_ids["PARENT_ID"] = $res["PARENT_ID"];
        }

        if ($res["GRANDPARENT_ID"] != "-1") {
            $marking_periods_ids["GRANDPARENT_ID"] = $res["GRANDPARENT_ID"];
            $has_grand = true;
        }
        $marking_periods_ids["MARKING_PERIOD_ID"] = $res["MARKING_PERIOD_ID"];

        if (!$has_grand) {
            $sql = "select * from marking_periods where marking_periods.SCHOOL_ID='" . UserSchool() . "' AND marking_periods.SYEAR='" . UserSyear() . "' and (PARENT_ID='" . $data["MARKING_PERIOD_ID"] . "' or  GRANDPARENT_ID='" . $data["MARKING_PERIOD_ID"] . "' ) ";

            $res = DBGet(DBQuery($sql));
            foreach ($res as $value) {
                $marking_periods_ids[] = $value["MARKING_PERIOD_ID"];
            }
        }


        $sql = "select course_periods.TITLE from course_periods,courses where
       course_periods.course_id = courses.course_id 
        and
	    course_periods.school_id = courses.school_id 
        and
	    course_periods.marking_period_id = courses.marking_period_id_ 
	    and
	    course_periods.syear = courses.syear
        and
        course_periods.teacher_id='" . $data["TEACHER_ID_"] . "'
        $extra
        and
        course_periods.period_id='" . $data["PERIOD_ID"] . "'
       " . $data['STR_DAYS'] . "
       and 
       course_periods.marking_period_id in (" . implode(",", $marking_periods_ids) . ")
       and course_periods.SCHOOL_ID='" . UserSchool() . "' AND course_periods.SYEAR='" . UserSyear() . "'  ";



        $res = DBGet(DBQuery($sql));
        $res = $res[1];

        $teacher_info = $res["TITLE"];
        if (count($res) > 0)
            return true;
    }
    return false;
}
